<script setup lang="ts">
import { ref, reactive, toRaw, onMounted } from "vue";
import { useUserStore } from "../../stores/user";
import { useRouter } from "vue-router";

const router = useRouter();
const userStore = useUserStore();
const loginFormRef = ref();
const readonly = ref(true);
const errorMsg = ref<string>();
const loading = ref<boolean>(false);
const loginForm = reactive({
  username: "",
  password: "",
  remember: false,
});

const loginRules = ref({
  username: [{ required: true, message: "请输入用户名", trigger: "change" }],
  password: [{ required: true, message: "请输入密码", trigger: "change" }],
});

const cancelReadOnly = () => {
  readonly.value = false;
};

const onLogin = () => {
  errorMsg.value = "";
  // router.push('/')
  loginFormRef.value.validate((valid: boolean) => {
    if (!valid) {
      return false;
    }

    loading.value = true;
    const { password, ...resProps } = toRaw(loginForm);
    loading.value = false;
    router.push("/");
    //用户登录
    userStore
      .loginAction({ password: window.btoa(password), ...resProps })
      .then(() => router.push("/"))
      .catch(({ message }: Error) => {
        errorMsg.value = message;
      })
      .finally(() => {
        loading.value = false;
      });
  });
};
</script>

<template>
  <el-form
    ref="loginFormRef"
    :model="loginForm"
    :rules="loginRules"
    @keyup.enter="onLogin"
  >
    <div class="login-title">科技兴安综合管理平台</div>
    <el-form-item prop="username">
      <el-input v-model="loginForm.username" placeholder="用户名" />
    </el-form-item>
    <el-form-item prop="password">
      <!-- 阻止密码回填 -->
      <el-input tabindex="-1" show-password style="height: 0; width: 0" />
      <el-input v-model="loginForm.password" show-password placeholder="密码" />
    </el-form-item>
    <el-form-item prop="remember" class="login-remember-password">
      <el-checkbox v-model="loginForm.remember">30天内免登陆</el-checkbox>
    </el-form-item>
    <div class="error-tip">
      <span>{{ errorMsg }}</span>
    </div>
    <el-form-item class="login-button">
      <el-button
        :loading="loading"
        :disabled="loading"
        type="primary"
        @click="onLogin"
      >
        登录
      </el-button>
    </el-form-item>
  </el-form>
</template>

<style lang="scss" scoped>
.login-title {
  display: flex;
  justify-content: center;
  margin-bottom: 42px;
  font-size: 24px;
  line-height: 28px;
  color: var(--el-color-primary);
}

.login-remember-password {
  :deep(.el-checkbox__label) {
    font-size: 12px;
  }
}

.login-button {
  :deep(.el-button--primary) {
    width: 100%;
    font-size: 16px;
    letter-spacing: 8px;
  }
}

.error-tip {
  font-size: 12px;
  text-align: center;
  color: var(--el-color-error);
  height: 35px;
  margin-bottom: 5px;
  display: flex;
  justify-content: center;
  align-items: flex-end;
}

:deep(.el-form-item) {
  margin: 20px;

  .el-input__wrapper {
    box-shadow: none !important;
    border-radius: 0;
    border-bottom: 1px solid #ccc;
    padding: 0 10px 0 0;
  }

  &.is-error {
    .el-input__wrapper {
      border-bottom-color: var(--el-color-error);
    }
  }
}
</style>
